const BannerMongo = require("@mongodb/banner/index")
const FeedbackMongo = require("@mongodb/feedback/index")
const HomeLinkMongo = require("@mongodb/homeLink/index")
const MeMongo = require("@mongodb/me/index")
const PackageMongo = require("@mongodb/package/index")
const ProductionMongo = require("@mongodb/production/index")
const StudyTimeLineMongo = require("@mongodb/studyTimeLine/index")
const StudyMongo = require("@mongodb/study/index")

const { stateKeys } = require('@mongodb/base/model')

class Server {

	constructor() {}

	/**
	 * 获取数据总个数
	 * @param ctx 环境上下文
	 */
	static async dataCount (ctx) {
		// 正常状态
		const normalP = {
			$or: [
				{stateKey: stateKeys.NORMAL},
				{stateKey: {$exists: false}}
			]
		}
		// 回收站状态
		const dustbinP = { stateKey: stateKeys.DUSTBIN }
			// 删除状态
		const deleteP = { stateKey: stateKeys.DELETE }
		const dataArr = await Promise.all([
			BannerMongo.count(normalP), BannerMongo.count(dustbinP), BannerMongo.count(deleteP),
			FeedbackMongo.count(normalP), FeedbackMongo.count(dustbinP), FeedbackMongo.count(deleteP),
			HomeLinkMongo.count(normalP), HomeLinkMongo.count(dustbinP), HomeLinkMongo.count(deleteP),
			PackageMongo.count(normalP), PackageMongo.count(dustbinP), PackageMongo.count(deleteP),
			ProductionMongo.count(normalP), ProductionMongo.count(dustbinP), ProductionMongo.count(deleteP),
			StudyTimeLineMongo.count(normalP), StudyTimeLineMongo.count(dustbinP), StudyTimeLineMongo.count(deleteP),
			StudyMongo.count(normalP), StudyMongo.count(dustbinP), StudyMongo.count(deleteP),
			MeMongo.work_count()
		])

		const arr = [
			{ title: 'Banner' },
			{ title: '意见反馈' },
			{ title: 'HomeLink' },
			{ title: '封装' },
			{ title: '作品' },
			{ title: '学习时间线' },
			{ title: '学习总结' },
			{ title: '工作' }
		]

		for (let i = 0; i < dataArr.length; i++) {
			if (i === dataArr.length - 1) {
				const lastItem = arr[arr.length - 1]
				lastItem.allCount = dataArr[i].allCount
				lastItem.normalCount = dataArr[i].normalCount
				lastItem.dustbinCount = dataArr[i].dustbinCount
				lastItem.deleteCount = dataArr[i].deleteCount
			} else {
				const index = parseInt(i / 3)
				const type = i % 3
				switch (type) {
					case 0:
						arr[index].normalCount = dataArr[i]
						break
					case 1:
						arr[index].dustbinCount = dataArr[i]
						break
					case 2:
						arr[index].deleteCount = dataArr[i]
						arr[index].allCount = arr[index].normalCount + arr[index].dustbinCount + arr[index].deleteCount
						break
				}
			}
		}
		return arr
	}
}

module.exports = Server
